package com.demo.mircoauth.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import lombok.Builder;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

/**
 * oauth2 client
 * @TableName sys_client
 */
@TableName(value ="sys_client")
@Builder(toBuilder = true)
@Data
public class SysClient implements Serializable, ClientDetails {
    /**
     *
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 生成的client_id 即接入方的appKey
     */
    private String clientId;

    /**
     * 接入方的client_secret 即appSecret
     */
    private String clientSecret;

    /**
     * 作用域
     */
    private String scope;

    /**
     * 授权类型
     */
    private String authorizedGrantTypes;

    /**
     * 调用成功后返回的uri
     */
    private String redirectUri;

    /**
     * 客户端权限
     */
    private String authorities;

    /**
     * accessToken时效
     */
    private Integer accessTokenValidity;

    /**
     * refreshToken时效性
     */
    private Integer refreshTokenValidity;

    /**
     * 备注信息
     */
    private String additionalInformation;

    /**
     * 是否自动批准
     */
    private Boolean autoApprove;

    private String resourceIds;
    /**
     * 客户端应用图标
     */
    private String icon;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        return getSet(resourceIds);
    }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    @Override
    public String getClientSecret() {
        return clientSecret;
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    @Override
    public Set<String> getScope() {
        return getSet(scope);
    }

    public Set getSet(String scope) {
        if (scope != null) {
            return new HashSet<>(Arrays.asList(scope.split(",")));
        }else{
            return new HashSet<>();
        }

    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return getSet(authorizedGrantTypes);
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return getSet(redirectUri);
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        if (scope != null) {
            List<String> list = Arrays.asList(scope.split(","));
            List<GrantedAuthority> collect = list.stream().map(x -> new GrantedAuthority() {
                @Override
                public String getAuthority() {
                    return x;
                }
            }).collect(Collectors.toList());
            return collect;
        }else{
            return new ArrayList<GrantedAuthority>();
        }



    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValidity;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshTokenValidity;
    }

    @Override
    public boolean isAutoApprove(String s) {
        return autoApprove;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        HashMap<String, Object> map = new HashMap<>();
        return map;
    }
}

